﻿using MediatR;
using Yuekao.API.Read.Command;
using Yuekao.Domain.API;
using Yuekao.Domain.BaseReposi;
using Yuekao.Domain.Entitys;

namespace Yuekao.API.Read.CommandHandler
{
    public class AuditShowCommandHandler : IRequestHandler<AuditShowCommand, APIResult<APIPagging<AuditModel>>>
    {
        private readonly IBaseRepository<AuditModel> baseRepository;
        public AuditShowCommandHandler(IBaseRepository<AuditModel> baseRepository)
        {
            this.baseRepository = baseRepository;
        }
        public Task<APIResult<APIPagging<AuditModel>>> Handle(AuditShowCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPagging<AuditModel>> result = new APIResult<APIPagging<AuditModel>>();
            result.Code = APIEnum.Success;
            result.Message = "我的审批待办列表显示成功";
            var list = baseRepository.GetAll().Where(x=>x.AuditId==request.UserId);
            var totaCount = list.Count();
            var pageCount = (int)Math.Ceiling(totaCount * 1.0 / request.pageSize);
            APIPagging<AuditModel> pagging = new APIPagging<AuditModel>();
            pagging.TotaCount = totaCount;
            pagging.PageCount = pageCount;
            pagging.PageData = list.OrderByDescending(x => x.CreateTime).Skip((request.pageIndex - 1)  * request.pageSize).Take(request.pageSize).ToList();
            result.Data = pagging;
            return Task.FromResult(result);
        }
    }
}
